﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Experimental.Input;

public class Test02 : MonoBehaviour
{
    public Image yButtonImage;
    public Image xButtonImage;
    public Image bButtonImage;
    public Image aButtonImage;
    public Image rightTriggerImage;
    public Image leftTriggerImage;
    public Image rightShoulderImage;
    public Image leftShoulderImage;
    public Image selectButtonImage;
    public Image startButtonImage;
    public Image leftStickButtonImage;
    public Image buttonEastImage;
    public Image buttonSouthImage;
    public Image buttonNorthImage;
    public Image buttonWestImage;
    public Image rightStickButtonImage;

    public Image leftStickImage;
    public Image rightStickImage;

    Gamepad gamepad;
    private void Start()
    {
        gamepad = Gamepad.current;
        Debug.LogFormat("{0}, {1}, {2}", gamepad.name, gamepad.displayName, gamepad.shortDisplayName);
    }

    void Update()
    {
        //Gamepad gamepad = InputSystem.AddDevice<Gamepad>();//无效
        //Gamepad gamepad = InputSystem.GetDevice<Gamepad>();//有效
        gamepad = Gamepad.current;//有效

        if (gamepad.leftStick.IsActuated())
        {
            leftStickImage.color = Color.red;
        }
        else
        {
            leftStickImage.color = Color.white;
        }

        if (gamepad.rightStick.IsActuated())
        {
            rightStickImage.color = Color.red;
        }
        else
        {
            rightStickImage.color = Color.white;
        }

        if (gamepad.yButton.isPressed)
        {
            Debug.Log("yButton is pressed.");
            yButtonImage.color = Color.red;
        }
        else
        {
            yButtonImage.color = Color.white;
        }

        if (gamepad.xButton.isPressed)
        {
            Debug.Log("xButton is pressed.");
            xButtonImage.color = Color.red;
        }
        else
        {
            xButtonImage.color = Color.white;
        }

        if (gamepad.bButton.isPressed)
        {
            Debug.Log("bButton is pressed.");
            bButtonImage.color = Color.red;
        }
        else
        {
            bButtonImage.color = Color.white;
        }

        if (gamepad.aButton.isPressed)
        {
            Debug.Log("aButton is pressed.");
            aButtonImage.color = Color.red;
        }
        else
        {
            aButtonImage.color = Color.white;
        }

        if (gamepad.rightTrigger.isPressed)
        {
            Debug.Log("rightTrigger is pressed.");
            rightTriggerImage.color = Color.red;
        }

        else
        {
            rightTriggerImage.color = Color.white;
        }
        if (gamepad.leftTrigger.isPressed)
        {
            Debug.Log("leftTrigger is pressed.");
            leftTriggerImage.color = Color.red;
        }
        else
        {
            leftTriggerImage.color = Color.white;
        }

        if (gamepad.rightShoulder.isPressed)
        {
            Debug.Log("rightShoulder is pressed.");
            rightShoulderImage.color = Color.red;
        }
        else
        {
            rightShoulderImage.color = Color.white;
        }

        if (gamepad.leftShoulder.isPressed)
        {
            Debug.Log("leftShoulder is pressed.");
            leftShoulderImage.color = Color.red;
        }
        else
        {
            leftShoulderImage.color = Color.white;
        }

        if (gamepad.selectButton.isPressed)
        {
            Debug.Log("selectButton is pressed.");
            selectButtonImage.color = Color.red;
        }
        else
        {
            selectButtonImage.color = Color.white;
        }

        if (gamepad.startButton.isPressed)
        {
            Debug.Log("startButton is pressed.");
            startButtonImage.color = Color.red;
        }
        else
        {
            startButtonImage.color = Color.white;
        }

        if (gamepad.leftStickButton.isPressed)
        {
            Debug.Log("leftStickButton is pressed.");
            leftStickButtonImage.color = Color.red;
        }
        else
        {
            leftStickButtonImage.color = Color.white;
        }

        if (gamepad.rightStickButton.isPressed)
        {
            Debug.Log("rightStickButton is pressed.");
            rightStickButtonImage.color = Color.red;
        }
        else
        {
            rightStickButtonImage.color = Color.white;
        }

        //if (gamepad.buttonEast.isPressed)
        //{
        //    Debug.Log("buttonEast is pressed.");
        //    buttonEastImage.color = Color.red;
        //}
        //else
        //{
        //    buttonEastImage.color = Color.white;
        //}

        //if (gamepad.buttonSouth.isPressed)
        //{
        //    Debug.Log("buttonSouth is pressed.");
        //    buttonSouthImage.color = Color.red;
        //}
        //else
        //{
        //    buttonSouthImage.color = Color.white;
        //}

        //if (gamepad.buttonNorth.isPressed)
        //{
        //    Debug.Log("buttonNorth is pressed.");
        //    buttonNorthImage.color = Color.red;
        //}
        //else
        //{
        //    buttonNorthImage.color = Color.white;
        //}

        //if (gamepad.buttonWest.isPressed)
        //{
        //    Debug.Log("buttonWest is pressed.");
        //    buttonWestImage.color = Color.red;
        //}
        //else
        //{
        //    buttonWestImage.color = Color.white;
        //}
        
        if (gamepad.dpad.right.isPressed)
        {
            Debug.Log("buttonEast is pressed.");
            buttonEastImage.color = Color.red;
        }
        else
        {
            buttonEastImage.color = Color.white;
        }

        if (gamepad.dpad.down.isPressed)
        {
            Debug.Log("buttonSouth is pressed.");
            buttonSouthImage.color = Color.red;
        }
        else
        {
            buttonSouthImage.color = Color.white;
        }

        if (gamepad.dpad.up.isPressed)
        {
            Debug.Log("buttonNorth is pressed.");
            buttonNorthImage.color = Color.red;
        }
        else
        {
            buttonNorthImage.color = Color.white;
        }

        if (gamepad.dpad.left.isPressed)
        {
            Debug.Log("buttonWest is pressed.");
            buttonWestImage.color = Color.red;
        }
        else
        {
            buttonWestImage.color = Color.white;
        }

    }
}
